$(function () {
    if ((localStorage.rdsUserLoginName != null) && (localStorage.rdsUserPassWord != null)) {
        $("#loginName").val(localStorage.rdsUserLoginName);
        $("#passWord").val(localStorage.rdsUserPassWord);
    }

    $('#form-login').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            loginName: {
                validators: {
                    notEmpty: {message: '登录账号不能为空！'},
                    stringLength: {min: 6, max: 30, message: '登录账号长度必须在6到30之间'}
                }
            },
            passWord: {
                validators: {
                    notEmpty: {message: '登录密码不能为空！'},
                    regexp: {regexp: /^[a-zA-Z0-9_\.]+$/, message: '登录密码只能是字母、数字和下划线'},
                    stringLength: {min: 6, max: 30, message: '登录密码长度必须在6到30之间'}
                }
            },
            inputCode: {
                validators: {
                    notEmpty: {
                        message: '验证码不能为空！'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        e.preventDefault();
        var $form = $(e.target);
        $.ajax({
            url: 'rds_user/loginCheck',
            type: 'post',
            data: $form.serialize(),
            dataType: 'json',
            success: function (msg) {
                if (msg != null && typeof (msg) != "undefined") {
                    if (msg.isSuccess == true) {
                        if (msg.jsonData.loginResult == 0) {
                            bootbox.alert({
                                message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">对不起，用户名错误，请重新输入</b>',
                                buttons: {
                                    ok: {
                                        label: '<i class="fa fa-check"></i> 确定',
                                        className: 'btn btn-primary btn-flat'
                                    }
                                }
                            });
                        } else if (msg.jsonData.loginResult == 1) {
                            bootbox.alert({
                                message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">对不起，密码错误，请重新输入</b>',
                                buttons: {
                                    ok: {
                                        label: '<i class="fa fa-check"></i> 确定',
                                        className: 'btn btn-primary btn-flat'
                                    }
                                }
                            });
                        } else if (msg.jsonData.loginResult == 2) {
                            if ($("#rememberMe").is(':checked') == true) {
                                localStorage.rdsUserLoginName = $("#loginName").val();
                                localStorage.rdsUserPassWord = $("#passWord").val();
                            }
                            sessionStorage.rdsUserLoginResult = JSON.stringify(msg.jsonData);
                            window.location.href = "webpage/homepage.html";
                        } else if (msg.jsonData.loginResult == 3) {
                            bootbox.alert({
                                message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">该账户已被停用,请与系统管理员联系</b>',
                                buttons: {
                                    ok: {
                                        label: '<i class="fa fa-check"></i> 确定',
                                        className: 'btn btn-primary btn-flat'
                                    }
                                }
                            });
                        } else if (msg.jsonData.loginResult == 4) {
                            bootbox.alert({
                                message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">该账户公司已被停用,请与系统管理员联系</b>',
                                buttons: {
                                    ok: {
                                        label: '<i class="fa fa-check"></i> 确定',
                                        className: 'btn btn-primary btn-flat'
                                    }
                                }
                            });
                        } else if (msg.jsonData.loginResult == 5) {
                            bootbox.alert({
                                message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">对不起，验证码错误，请重新输入</b>',
                                buttons: {
                                    ok: {
                                        label: '<i class="fa fa-check"></i> 确定',
                                        className: 'btn btn-primary btn-flat'
                                    }
                                }
                            });
                            $.refreshCode();
                        } else {
                            bootbox.alert({
                                message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">系统错误，请与系统管理员联系</b>',
                                buttons: {
                                    ok: {
                                        label: '<i class="fa fa-check"></i> 确定',
                                        className: 'btn btn-primary btn-flat'
                                    }
                                }
                            });
                            $('#form-login').bootstrapValidator('disableSubmitButtons', false).bootstrapValidator('resetForm', true);
                        }
                    }
                } else {
                    bootbox.alert({
                        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">系统错误，请于系统管理员联系</b>',
                        buttons: {
                            ok: {
                                label: '<i class="fa fa-check"></i> 确定',
                                className: 'btn btn-primary btn-flat'
                            }
                        }
                    });
                    $('#form-login').bootstrapValidator('disableSubmitButtons', false).bootstrapValidator('resetForm', true);
                }
            }
        });
    });
});


$.refreshCode = function () {
    $("#imgCode").attr("src", window.location.href.split('/')[0] + "/rds_captcha/getCaptcha?flag=" + Math.random());
    $("#inputCode").val("");
};